package fourmiefight.metier;

import java.util.ArrayList;

import org.dyn4j.geometry.Rectangle;

import fourmiefight.abstractionlib2d.CustomBody;
import fourmiefight.controller.Simulateur;
import fourmiefight.ihm.FourmiliereView;

public class Fourmiliere extends CustomBody
{
	private static final double WIDTH = 1.0;
	private static final double HEIGHT = 1.0;
	
	private static final int NB_TIC_BEFORE_FOOD_CONSUMPTION = 100;
	private int currentTicNumber = 0;
	
	// Permet de gérer l'autoincrément de l'id
	//static AtomicInteger nextId = new AtomicInteger();
	public static int currId = 0;
		
	// Attributs
	private int idd;
	private String nom;
	private int nbDepart;
	private int numEquipe;
	private int nourriture;
	private int porteePheromones;
	private int persistancePheromones;
	private int dureeVie;
	private int frequencePonte;
	private int couleur;
	private int maxFourmis;
	
	private ArrayList<Fourmi> listeFourmis;
	private ArrayList<Pheromone> listePhero;
	
	// M�thodes
	// - Constructeurs
	public Fourmiliere() 
	{
		intitialiseFourmiliere(50);
	}
	
	/**
	 * Constructeur en passant le nombre de fourmis
	 */
	public Fourmiliere(int nbFourmis) 
	{
		intitialiseFourmiliere(nbFourmis);
	}
	
	private void intitialiseFourmiliere(int nbFourmis)
	{
		
		this.setIdd(/*nextId.incrementAndGet()*/currId);
		currId++;
		
		this.setNbDepart(nbFourmis);
		
		nourriture = 50 * nbFourmis;
		this.setNom("Fourmilliere_" + this.getIdd());
		
		view = new FourmiliereView();
		
		Rectangle r = new Rectangle(WIDTH, HEIGHT);
		this.addFixture(r);
	}
	
	// - Actions
	public void ajouterFourmi(Fourmi fourmi) {
		if (this.listeFourmis == null) {
			
			this.listeFourmis = new ArrayList<>();
		}
		this.listeFourmis.add(fourmi);
	}
	
	public void supprimerFourmi(Fourmi fourmi) {
		listeFourmis.remove(fourmi);
	}
	
	public void ajouterPheromone(Pheromone phephe) {
		if(this.listePhero == null) {
			this.listePhero = new ArrayList<>();
		}
		this.listePhero.add(phephe);
	}
	
	public void supprimerPheromone(Pheromone phephe) {
		this.listePhero.remove(phephe);
	}
	
	// A voir car on a aussi la m�thode getNbFourmis
	public int getNbFourmis() {
		return this.listeFourmis.size();
	}
	
	public String toString(){
		return "Id : " + this.getIdd() + 
				"Nom : " + this.getNom();
	}
	
	// - Getteurs & Setteurs
	public int getIdd() {
		return idd;
	}

	public void setIdd(int idd) {
		this.idd = idd;
	}

	public String getNom() {
		return nom;
	}

	public void setNom(String nom) {
		this.nom = nom;
	}

	public int getNumEquipe() {
		return numEquipe;
	}
	
	public void setNumEquipe(int numEquipe) {
		this.numEquipe = numEquipe;
	}


	public int getNourriture() {
		return nourriture;
	}


	public void setNourriture(int nourriture) {
		this.nourriture = nourriture;
	}


	public int getPorteePheromones() {
		return porteePheromones;
	}


	public void setPorteePheromones(int porteePheromones) {
		this.porteePheromones = porteePheromones;
	}


	public int getPersistancePheromones() {
		return persistancePheromones;
	}


	public void setPersistancePheromones(int persistancePheromones) {
		this.persistancePheromones = persistancePheromones;
	}


	public int getDureeVie() {
		return dureeVie;
	}


	public void setDureeVie(int dureeVie) {
		this.dureeVie = dureeVie;
	}


	public int getFrequencePonte() {
		return frequencePonte;
	}


	public void setFrequencePonte(int frequencePonte) {
		this.frequencePonte = frequencePonte;
	}


	public int getCouleur() {
		return couleur;
	}


	public void setCouleur(int couleur) {
		this.couleur = couleur;
	}


	public int getMaxFourmis() {
		return maxFourmis;
	}


	public void setMaxFourmis(int maxFourmis) {
		this.maxFourmis = maxFourmis;
	}
	
    public int getNbDepart() {
		return nbDepart;
	}

	public void setNbDepart(int nbDepart) {
		this.nbDepart = nbDepart;
		this.listeFourmis = null;
		for (int i = 0; i < nbDepart; i++) 
		{
			this.ajouterFourmi(new Fourmi(this));	
		}
		
	}

	public ArrayList<Fourmi> getListeFourmis()
    {
    	return listeFourmis;
    }
    
    public void notifyATic()
    {
      
      if(Simulateur.getEtatSimulateur().equals(Simulateur.State.EN_COURS)) {
        currentTicNumber++;
      }
      
    	if(currentTicNumber >= NB_TIC_BEFORE_FOOD_CONSUMPTION)
    	{
    	  if(nourriture > 0 && currentTicNumber%20 == 0) {
    	    nourriture -= 10;
    	  }
    	  else if(nourriture <= 0){
    	    
    	    if(listeFourmis.size() > 0)
          {
            ArrayList<Fourmi> itemsToDestroy = new ArrayList<>();
            for(Fourmi fourmi : listeFourmis)
            {
                fourmi.setKillMe(true);
                fourmi.getWorld().removeBody(fourmi);
                itemsToDestroy.add(fourmi);
            }
            
            for(Fourmi f : itemsToDestroy)
            {
              listeFourmis.remove(f);
              // TODO : buter de la liste
            }
          }
          else if(this.getWorld() != null)
          {
            this.getWorld().removeBody(this);
          }
    	    
    	  }

    	}
    	
    	for(Fourmi f : listeFourmis)
    	{
    		f.notifyATic();
    	}
    	
    	if(listePhero != null && listePhero.size() > 0) {
			ArrayList<Pheromone> itemToDestroy = new ArrayList<>();
			for(Pheromone phephe : listePhero) {
				
				if(phephe.getPersistance() > 1) {
					phephe.setPersistance(phephe.getPersistance()-0.050);
				}
				else 
				{
					itemToDestroy.add(phephe);
					phephe.removeOfBody();
				}
			}
			
			for(Pheromone phephe : itemToDestroy) {
				listePhero.remove(phephe);
			}
			
		}
    }
    
    public void addFood(int food)
    {
    	nourriture += food;
    }
}
